package cn.itcast.controller;


import cn.itcast.service.IChatHistoryService;
import cn.itcast.vo.MessageVO;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    @Autowired
    private ChatMemoryRepository chatMemoryRepository;
    @Autowired
    private IChatHistoryService chatHistoryService;
    @Autowired
    private ChatMemory chatMemory;

    /**
     * 查询会话历史
     * @return
     */
    @GetMapping("/{type}")
    public List<String> selectHistoryChat(@PathVariable("type") String type){
        return chatHistoryService.getChatIds(type,null);
    }

    /**
     * 查询当前会话记录
     * @param type
     * @param chatId
     * @return
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> selectCurrentChat(@PathVariable("type") String type, @PathVariable("chatId") String chatId){
        List<Message> messageList=chatMemory.get(chatId);
        return messageList.stream().map(MessageVO::new).toList();
    }
}